<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Dayrui Website Management System
 *
 * @since	    version 2.7.0
 * @author	    Dayrui <dayrui@gmail.com>
 * @license     http://www.dayrui.com/license
 * @copyright   Copyright (c) 2011 - 9999, Dayrui.Com, Inc.
 */

class D_Member_Comment extends M_Controller {

    public $uri;
    public $rname;

    /**
     * 构造函数
     */
    public function __construct() {
        parent::__construct();
        $this->load->model('comment_model');
    }

    // 设置空间操作评论
    public function space() {
    }

    // 设置空间模型操作评论
    public function model($mid) {
    }

    // 设置模块操作评论
    public function module($dir) {
        $this->comment_model->module($dir);
        $this->uri = $dir.'/comment/index';
        $this->rname = 'comment-module-'.$dir;
    }

    // 设置模块扩展操作评论
    public function extend($dir) {
        $this->comment_model->extend($dir);
        $this->uri = $dir.'/ecomment/index';
        $this->rname = 'comment-module-'.$dir;
    }

    // 我评论过的信息
    public function index() {

        // 接收参数
        $kw = dr_safe_replace($this->input->get('kw', TRUE));
        $order = dr_get_order_string(isset($_GET['order']) && strpos($_GET['order'], "undefined") !== 0 ? $this->input->get('order', TRUE) : 'id desc', 'id desc');

        // 组合条件
        $this->comment_model->mydb->where('uid', $this->uid);
        if ($kw) {
            $this->comment_model->mydb->like('title', $kw);
        }
        $this->comment_model->mydb->order_by($order);
        $page = max((int)$this->input->get('page'), 1);
        $tpl = is_file(APPPATH.'templates/member/'.MEMBER_TEMPLATE.'/'.$this->rname.'_data.html') ? $this->rname.'_data.html' : 'comment_data.html';

        if ($this->input->get('action') == 'search') {
            // ajax请求
            $data = $this->comment_model->mydb->limit($this->pagesize, $this->pagesize * ($page - 1))->get($this->comment_model->prefix.'_comment_my')->result_array();
            if (!$data) {
                exit('null');
            }
            $this->template->assign(array(
                'list' => $data,
            ));
            $this->template->display($tpl);
        } else {
            // 首次请求
            $this->template->assign(array(
                'page' => max(2, $page + 1),
                'list' => $this->comment_model->mydb->limit($page ? $page * $this->pagesize : $this->pagesize)->get($this->comment_model->prefix.'_comment_my')->result_array(),
                'moreurl' => dr_member_url($this->uri).'&action=search&kw='.$kw.'&order='.$order,
                'datatpl' => $tpl,
                'meta_name' => fc_lang('我评论过的'),
            ));
            $this->template->display(is_file(APPPATH.'templates/member/'.MEMBER_TEMPLATE.'/'.$this->rname.'.html') ? $this->rname.'.html' : 'comment_index.html');
        }
    }


}
